/**
 * 用于生成slidbar
 * Created by stylewolf on 2016/1/27.
 */

(function($){
    "use strict";
    var nav = $("#nav-list");
    //单个li的模板
    var tplLi = [
        '<li id="@{id}" class="sidebar-leaf">',
        '    <a href="#" url="@{url}" mid="@{id}" label="@{name}" owner="@{owner}" class="menu-a">',
        '        <i class="menu-icon fa fa-caret-right"></i>',
        '        <span class="menu-text"><i class="menu-icon fa @{icon}"></i>&nbsp;&nbsp;@{name}</span>',
        '    </a>',
        '    <b class="arrow"></b>',
        '</li>'
    ].join('');
    var compiled_tp_li = juicer(tplLi);
    //拥有子集菜单的处理
    var tplMenu = [
        '<li id="@{id}">',
        '    <a href="#" class="dropdown-toggle" label="@{name}">',
        '        <i class="menu-icon fa @{icon}"></i>',
        '        <span class="menu-text">',
        '        @{name}',
        '        </span>',
        '        <b class="arrow fa fa-angle-down"></b>',
        '    </a>',
        '    <b class="arrow"></b>',
        '    <ul class="submenu">',
        '    </ul>',
        '</li>'
    ].join('');
    //处理菜单点击效果
    $(document).on("click","li.sidebar-leaf",function(){
       nav.find("li").removeClass("active");
       $(this).parents("li:first").addClass("active");
       $(this).addClass("active");
    });
    var compiled_tp_menu = juicer(tplMenu);
    $.loadSideBar = function(){
        $.request($.router.module.queryLogin,{
            callback:function(json){
            	nav.html("");
                $("#user-name").html(json.login.userName);
                $.each(json.datas,function(i,d){
                    //如果有上级
                    if(d.owner){
                        var $li = $(compiled_tp_li.render(d));
                        $("#"+d.owner).find("ul.submenu").append($li);
                    }else{
                        switch (d.leaf){
                            case 0 :
                                var $ul = $(compiled_tp_menu.render(d));
                                nav.append($ul);
                                break;
                            case 1 :
                                var $li = $(compiled_tp_li.render(d));
                                nav.append($li);
                                break;
                            default :
                                break;

                        }
                    }
                });
            }
        });
    }
    
})(jQuery);
$(function(){
    //退出登录
    $("#power-off").click(function(){
    	$.request($.router.loginOut,{
    		callback:function(json){
    			window.location.href=$.router.html.login;
    		}
		});
    });
    var $pathGuide = $("#b-dashboard");
    var $breadcrumb = $(".breadcrumb");
    $.loadSideBar();
	$(document).on("click",".menu-a",function(){
		var $this = $(this);
		$.loadPage($.router.server + $this.attr('url')+"?t="+new Date().getTime(),{
			callback:function(el){
				el.trigger("pageshow."+$this.attr('mid'));
				if($this.attr("owner")){
					$breadcrumb.children(":not(:first)").remove();
					var $p = $("#"+$this.attr("owner"));
					$breadcrumb.append('<li>'+$p.find(".dropdown-toggle").attr("label")+'</li>');
					$breadcrumb.append('<li class="active" id="b-dashboard">'+$this.attr("label")+'</li>');
				}
				else{
					$pathGuide.html($this.attr("label"));
				}
			},
			el:"#page-content"
		});
	});
});
